package com.wm.machine.baselibrary.utils;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Author： Administrator
 * CreatTime： 2018/1/22
 * ContactInfo：
 * Description: 软键盘工具类
 */

public final class KeyBroadUtils {
  private KeyBroadUtils() {

  }

  /**
   * 打开软键盘
   *
   * @param mEditText 输入框
   * @param mContext 上下文
   */
  public static void open(EditText mEditText, Context mContext) {
    InputMethodManager imm =
        (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mEditText, InputMethodManager.RESULT_SHOWN);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
  }

  /**
   * 关闭软键盘
   *
   * @param mEditText 输入框
   * @param mContext 上下文
   */
  public static void close(EditText mEditText, Context mContext) {
    InputMethodManager imm =
        (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
  }

  /**
   * 强制隐藏输入法键盘
   * 
   * @param edittext 输入框
   */
  public static void hide(EditText edittext) {
    InputMethodManager inputMethodManager =
        (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager.isActive()) {
      inputMethodManager.hideSoftInputFromWindow(edittext.getWindowToken(), 0);
    }
  }

  /**
   * 通过定时器强制隐藏虚拟键盘
   * 
   * @param v view 附的view
   * @param time  延迟时间
   */
  public static void postDelayHide(final View v, int time) {
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
      @Override
      public void run() {
        InputMethodManager imm =
            (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm.isActive()) {
          imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
        }
      }
    }, time);
  }

  /**
   * 键盘是否显示
   * 
   * @param edittext 获取焦点的edittext
   * @return boolean 显示为true;
   */
  public static boolean isShow(EditText edittext) {
    boolean bool = false;
    InputMethodManager imm =
        (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (imm.isActive()) {
      bool = true;
    }
    return bool;
  }

  /**
   * 切换软键盘的状态
   * 如当前为收起变为弹出,若当前为弹出变为收起
   * 
   * @param edittext 获取焦点的edittext
   */
  public static void toggleStatus(EditText edittext) {
    InputMethodManager inputMethodManager =
        (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
  }

  /**
   * 强制显示输入法键盘
   * 
   * @param edittext 获取焦点的edittext
   */
  public static void showKeybord(EditText edittext) {
    InputMethodManager inputMethodManager =
        (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.showSoftInput(edittext, InputMethodManager.SHOW_FORCED);
  }

  /**
   * 输入法是否显示
   * 
   * @param edittext 获取焦点的edittext
   * @return boolean 显示为true
   */
  public static boolean isShowInputMethod(EditText edittext) {
    boolean bool = false;
    InputMethodManager inputMethodManager =
        (InputMethodManager) edittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    if (inputMethodManager.isActive()) {
      bool = true;
    }
    return bool;
  }

  /**
   * 隐藏输入法
   *
   * @param mAct activity
   */
  public static void hideInputMethod(Activity mAct) {
    try {
      View v = mAct.getWindow().getCurrentFocus();
      if (v != null) {
        InputMethodManager imm =
            (InputMethodManager) mAct.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
      }
    } catch (Exception e) {

    }
  }

  /**
   * 显示输入法
   *
   * @param mAct activity
   */
  public static void showInputMethod(final Activity mAct) {
    View v = mAct.getCurrentFocus();
    if (null == v) {
      return;
    }
    ((InputMethodManager) mAct.getSystemService(Activity.INPUT_METHOD_SERVICE)).showSoftInput(v, 0);
  }

}
